package com.overlord.im.server.resolver.message;

import com.overlord.im.commons.constant.AckType;
import com.overlord.im.commons.message.OverlordImMessage;
import com.overlord.im.server.message.entity.ServerMessageEntity;
import com.overlord.im.server.message.service.ServerMessageService;
import com.overlord.im.server.message.service.SendMessageService;
import com.overlord.im.server.resolver.MessageResolver;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName AckrResolver
 * @Author wuwei
 * @Description NONE
 * @Date 2020/6/2 18:03
 **/
@Service
public class AckrResolver implements MessageResolver<OverlordImMessage> {

    @Resource
    private ServerMessageService serverMessageService;

    @Resource
    private SendMessageService sendMessageService;

    @Override
    public boolean support(OverlordImMessage message) {
        return message.getAckType() == AckType.ACK_R;
    }

    @Override
    public void resolve(OverlordImMessage message) {
        Long sequenceId = message.getSequenceId();
        ServerMessageEntity entity = serverMessageService.findBySequenceId(sequenceId);
        //给接收方发送ACK_A消息
        sendMessageService.sendAckA(message, entity);
        //给发送方发送ACK_N
        sendMessageService.sendAckN(message, entity);
    }

}
